
const model = require('../schems')
const md5 =require('md5-node')


//初始化管理员账号
async function  init(){
await model.user.findOne({name:'admin'},function(err,result){
    if(!result){
      config.adminConfig.password = md5(md5(config.adminConfig.password))
      let admin =new model.user(config.adminConfig)
       admin.save().then(
        function(result){
          config.defaultCate.userId=result._id
          return;
        }
      )
    }else{
      config.defaultCate.userId= result._id
    }
     
  })

  await model.category.findOne({name:'博客'},function(err,result){
    if(result!= null){
      if(result.cateid == 1){
        console.log('初始化分类已存在')
        return
      }
    }else{
      console.log('初始化分类')
      let category = new model.category(config.defaultCate)
          category.save()
    }
  })
}

//init()


let ueserAction = {}
// 处理用户注册
ueserAction.regiter = async function (obj, callback) {
  await model.user.findOne({ name: obj.name }, function (err, result) {
    if (result) {
      callback(false)
      return
    }
  })
  obj.password = md5(md5(obj.password ))
  let user = new model.user(obj);
  await user.save().then((e) => callback(e))
}

//用户登录
ueserAction.login = async function (obj, callback) {
  await model.user.findOne({
    name: obj.name,
  }, function (err, result) {
    if (result) {
      //比较密码
      if(result.password  ==md5(md5(obj.password))){
        callback(result)
      }else{
        callback(false)
      }

    }
  })
}

//用户资料更新
ueserAction.updata = async function (name,obj, callback) {

  if(obj.password==''){//如果未修改密码使用原来的密码
  await model.user.findOne({name},function(err,result){
     if(result){
      obj.password = result.password
     }
    })
  }
  await model.user.updateOne({name},obj,function(err,result){
    if(result){
      callback(result)
    }
  })
}

//添加用户
ueserAction.add = function (obj, callback) {
  let user =new model.user(obj)
  user.save().then(e=>callback(e))
}


//删除用户
ueserAction.delete = function (user, callback) {
  model.user.deleteOne({
    'name': user },function(err){
      if(!err){
        callback(true)
      }
    })
}


function countArt(id){
  return new Promise(function(resolve, reject){
    model.article.find({"authorId":id}).count({},function(err,count){ 
      if(!err){
      return resolve(count)
      }
    })
  })
}

//查詢所有賬戶
ueserAction.all = function (callback) {
  model.user.find({},function(err,doc){
    if(err){
      return callback(false)
    }else{
      var user = JSON.parse(JSON.stringify(doc))
      //查询是真慢
      Promise.all(user.map((item)=>countArt(item._id)
      )).then(function(doc){
        for(let i=0;i<doc.length;i++){
          user[i].artcount = doc[i]
        }
        callback(user)
      }) 
    }
  })
}

//查询账户相关信息
ueserAction.getOne = function (name,callback) {
  model.user.findOne({name},function(err,doc){
    if(err){
      return callback(false)
    }else{
      doc.password='' //移除密码
      callback(doc)
    }
  })
}



module.exports = ueserAction 